/**
 * Copyright millionhere
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You may see it at...
 *      http://www.apache.org/licenses/LICENSE-2.0
 */

package info.millionhere.keepsilent;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class PickupConversationActivity extends ListActivity{
    
//    private ListView mConversationView;
    
    private ArrayList<HashMap<String, Object>> conversationList;
    
    static final String TAG_ADDRESS = "ADDRESS";
    static final String TAG_MESSAGE = "COMMENT";
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        
        conversationList = new ArrayList<HashMap<String, Object>>();
        
        try {
            Cursor conversationCursor = getContentResolver().query(Uri.parse("content://mms-sms/conversations?simple=true"), null, null, null, null);

            if (conversationCursor.moveToFirst()){
        	int idAddress = conversationCursor.getColumnIndex("recipient_address");
        	int idBody = conversationCursor.getColumnIndex("body");

        	do{
        	    String address = conversationCursor.getString(idAddress);
        	    String message = conversationCursor.getString(idBody);
        	    if (message.equals("")){
        		message = getString(R.string.text_pickupconversation_mms);
        	    }
        	    conversationList.add(new ConversationItem(address, message).Print());
        	}
        	while(conversationCursor.moveToNext());
            }
        }
        catch (Exception e){}
        finally {
            if (conversationList.isEmpty()){
        	conversationList.add(new ConversationItem("", "").Print());
            }
        }
        
        this.setListAdapter(new SimpleAdapter(
        	this,
        	conversationList,
                R.layout.list_item_with_two_row,
                new String[] { TAG_ADDRESS, TAG_MESSAGE },
                new int[] { R.id.text_conversation_address, R.id.text_conversation_message }
        	)
        );
        
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id){
	Intent intent = new Intent();
	Bundle bundle = new Bundle();
	try{
	    bundle.putString(TAG_ADDRESS, conversationList.get(position).get(TAG_ADDRESS).toString());
	    bundle.putString(TAG_MESSAGE, conversationList.get(position).get(TAG_MESSAGE).toString());
	}
	catch(Exception e){
	    this.setResult(RESULT_CANCELED, intent);
	}
	intent.putExtras(bundle);
	this.setResult(RESULT_OK, intent);
	this.finish();
    }
    
    private static class ConversationItem{
	String address;
	String message;
	
	public ConversationItem(String address,String message){
	    this.address = address;
	    this.message = message;
	}
	
	public HashMap<String, Object> Print(){
	    HashMap<String, Object> t = new HashMap<String, Object>();
	    t.put(TAG_ADDRESS, address);
	    t.put(TAG_MESSAGE, message);
	    return t;
	}
    }
}
